package com.tulele.vtool.service.impl;

import com.tulele.vtool.mapper.BPlatformBeginnerArticleTypeMapper;
import com.tulele.vtool.model.PlatformSysInfo;
import com.tulele.vtool.model.combinedEntity.BBeginnerSetting;
import com.tulele.vtool.model.combinedEntity.BeginnerArticleInfo;
import com.tulele.vtool.model.combinedEntity.PlatformArticleType;
import com.tulele.vtool.service.BPlatformBeginnerArticleTypeService;
import com.tulele.vtool.service.SysGzhService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("bPlatformBeginnerArticleTypeService")
@Slf4j
public class BPlatformBeginnerArticleTypeServiceImpl implements BPlatformBeginnerArticleTypeService {
    @Autowired
    private SysGzhService sysGzhService;

    @Autowired
    private BPlatformBeginnerArticleTypeMapper bPlatformBeginnerArticleTypeMapper;

    @Override
    public List<PlatformArticleType> getPlatformArticleType(String gzhCode) {
        return bPlatformBeginnerArticleTypeMapper.getPlatformArticleType(gzhCode);
    }

    @Override
    public List<BeginnerArticleInfo> getPlatformArticleInfo(String identifying, Integer articleTypeId, Integer displayCount) {
        PlatformSysInfo platformSysInfo = sysGzhService.getPlatformSysInfo(identifying);
        String articleDetailUrl = platformSysInfo.getArticleDetailUrl();

        log.info("[articleDetailUrl] = {}", articleDetailUrl);

        List<BeginnerArticleInfo> platformArticleInfo = bPlatformBeginnerArticleTypeMapper.getPlatformArticleInfo(identifying, articleTypeId, displayCount);

        // 构造访问路径
        if (CollectionUtils.isNotEmpty(platformArticleInfo)) {
            platformArticleInfo.forEach((info) -> {
                if (!info.getArticleId().equals(-1)) {
                    String accessUrl = articleDetailUrl + info.getArticleId();
                    if (info.getResourceType().equals(2)) {//如果是视频
                        accessUrl = accessUrl.replace("/article/","/video/");
                        accessUrl = accessUrl.replace("/detail.html?","/index.html?");
                    }

                    info.setAccessUrl(accessUrl);
                }
            });
        }
        return platformArticleInfo;
    }

    @Override
    public BBeginnerSetting getBeginnerPageInfo(String gzhCode) {
        return bPlatformBeginnerArticleTypeMapper.getBeginnerPageInfo(gzhCode);
    }

    @Override
    public void addBeginnerLookArticle(String gzhCode) {
        bPlatformBeginnerArticleTypeMapper.addBeginnerLookArticle(gzhCode);
    }
}
